﻿using System.Collections.Generic;
using System;
using System.Text;

namespace CodeSmithCore.Helpers
{
    public class VbaScriptBuilder
    {
        private readonly List<StandardField> _fields = new List<StandardField>();
        private string _tableComment = "";
        private string _tableName = "Table_11";

        public class StandardField
        {
            public string Code { get; set; }
            public string Name { get; set; }
            public string Comment { get; set; }
            public string DataType { get; set; }
            public bool IsPrimaryKey { get; set; }
            public bool Mandatory { get; set; }
            public string DefaultValue { get; set; }
        }

        public VbaScriptBuilder AddStandardField(StandardField field)
        {
            _fields.Add(field);
            return this;
        }

        public VbaScriptBuilder ConfigureTableFilter(string tableName, string tableComment)
        {
            _tableName = tableName;
            _tableComment = tableComment;
            return this;
        }

        public string Build()
        {
            var sb = new StringBuilder();

            // 生成脚本头部
            sb.AppendLine("Option Explicit");
            sb.AppendLine("ValidationMode = True");
            sb.AppendLine("InteractiveMode = im_Batch");
            sb.AppendLine("' Generated by C# Code Generator\n");

            // 主程序逻辑
            sb.AppendLine("Dim mdl");
            sb.AppendLine("Set mdl = ActiveModel");
            sb.AppendLine("If (mdl Is Nothing) Then");
            sb.AppendLine("   MsgBox \"There is no Active Model\"");
            sb.AppendLine("Else");
            sb.AppendLine("   ListObjects(mdl)");
            sb.AppendLine("End If");
            sb.AppendLine();

            // 生成ListObjects过程
            sb.AppendLine("'-----------------------------------------------------------------------------");
            sb.AppendLine("Private Sub ListObjects(fldr)");
            sb.AppendLine("   output \"Scanning \" & fldr.code");
            sb.AppendLine("   Dim obj");
            sb.AppendLine("   For Each obj In fldr.children");
            sb.AppendLine("      TableSetComment obj");
            sb.AppendLine("   Next");
            sb.AppendLine();
            sb.AppendLine("   Dim f");
            sb.AppendLine("   For Each f In fldr.Packages");
            sb.AppendLine("      ListObjects f");
            sb.AppendLine("   Next");
            sb.AppendLine("End Sub");
            sb.AppendLine();

            // 生成TableSetComment过程
            sb.Append(BuildTableSetComment());
            return sb.ToString();
        }

        private string BuildTableSetComment()
        {
            const string Variable = "Variable";
            var sb = new StringBuilder();

            sb.AppendLine("Private Sub TableSetComment(CurrentObject)");
            sb.AppendLine("   If Not CurrentObject.Iskindof(cls_Table) Then Exit Sub");
            sb.AppendLine();

            // 表过滤逻辑

            sb.AppendLine($"   If InStr(\",{_tableName},\",\",\" & CurrentObject.Code & \",\") <= 0 Then Exit Sub");

            sb.AppendLine("   output \"添加统一字段：\" & CurrentObject.Code");
            sb.AppendLine($"  CurrentObject.Name = \"{_tableComment}\"");
            sb.AppendLine($"  CurrentObject.Comment = \"{_tableComment}\"");
            sb.AppendLine("   If Not CurrentObject.isShortcut Then");
            sb.AppendLine("      Dim col");
            sb.AppendLine("      Dim num");
            sb.AppendLine();

            // 生成字段检查变量
            foreach (var field in _fields)
            {
                sb.AppendLine($"      Dim {Variable}{field.Code}");
                sb.AppendLine($"      {Variable}{field.Code} = 1");
            }
            sb.AppendLine();

            // 生成字段检查逻辑
            sb.AppendLine("      For Each col In CurrentObject.columns");
            sb.AppendLine("          num = num + 1");
            foreach (var field in _fields)
            {
                sb.AppendLine($"          If col.Code = \"{field.Code}\" Then");
                sb.AppendLine($"              {Variable}{field.Code} = 100");
                sb.AppendLine("          End If");
            }
            sb.AppendLine("      Next");
            sb.AppendLine();

            // 生成字段创建逻辑
            foreach (var field in _fields)
            {
                sb.AppendLine($"      If {Variable}{field.Code} = 1 Then");
                sb.AppendLine("          CurrentObject.columns.CreateNewAt(num)");
                sb.AppendLine("          For Each col In CurrentObject.columns");
                sb.AppendLine("              If col.DataType = \"\" Then");
                sb.AppendLine($"                  col.Name = \"{field.Name}\"");
                sb.AppendLine($"                  col.Code = \"{field.Code}\"");
                sb.AppendLine($"                  col.Comment = \"{field.Comment}\"");
                sb.AppendLine($"                  col.DataType = \"{field.DataType}\"");
                if (field.IsPrimaryKey)
                    sb.AppendLine("                  col.Primary = True");
                if (field.Mandatory)
                    sb.AppendLine($"                  col.Mandatory = {Convert.ToInt32(field.Mandatory)}");
                if (!string.IsNullOrEmpty(field.DefaultValue))
                    sb.AppendLine($"                  col.DefaultValueDisplayed = \"{field.DefaultValue}\"");
                sb.AppendLine("              End If");
                sb.AppendLine("          Next");
                sb.AppendLine("          num = num + 1");
                sb.AppendLine("      End If");
                sb.AppendLine();
            }

            sb.AppendLine("   End If");
            sb.AppendLine("End Sub");

            return sb.ToString();
        }
    }


}
